
import { _decorator, Component, Node, EventTouch, view, director, UITransform, input, Input, EventKeyboard, KeyCode } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = GameInt
 * DateTime = Sun Apr 17 2022 14:41:45 GMT+0800 (中国标准时间)
 * Author = xiaopang125
 * FileBasename = GameInt.ts
 * FileBasenameNoExtension = GameInt
 * URL = db://assets/GameInt.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('GameInt')
export class GameInt extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;
    moveNum = [1, 1];
    heroMove = false;
    @property({ type: Node })
    hero: Node = null;
    start() {
        // [3]
        this.node.on(Node.EventType.TOUCH_START, this.gameInt, this);
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_PRESSING, this.onKeyPRESSING, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    }
    /**
     * gameInt
e   
  */
onKeyPRESSING(event: EventKeyboard) {
    this.heroMove = true;}
    onKeyDown(event: EventKeyboard) {
        this.heroMove = true;
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this.moveNum[1] = 1;
                break;
            case KeyCode.KEY_S:
                this.moveNum[1] = -1;
                break;
            case KeyCode.KEY_A:
                this.moveNum[0] = -1;
                break;
            case KeyCode.KEY_D:
                this.moveNum[0] = 1;
                break;
        }
      

    }
    onKeyUp(event: EventKeyboard) {
       
       
            this.heroMove = false;

        // switch (event.keyCode) {
        //     case KeyCode.KEY_A:
        //         console.log('Release a key');
        //         break;
        // }
    }
    public gameInt(e: EventTouch) {
        // console.log(this.node.position.x);
        // console.log(e);
        var delta = e.getDelta();
        var pos = e.getLocation();
        pos = e.getUILocation();
        console.log(e.getLocation().y);
        console.log(e.getUILocation().y);

        // console.log(director.getScene());
        // console.log(director.getTotalTime());

        // const pos2= this.getComponent(UITransform).convertToWorldSpaceAR((pos.x,pos.y,0));
        // console.log(e.getUILocation());  // Location on UI space


        // console.log(view.getDesignResolutionSize());  // Location on UI space

        // this.hero.setPosition( (this.node.position.x-pos.x)*-1, (this.node.position.y-pos.y)*-1,0);
        //  this.hero.setPosition(pos.x-cc.WinSize.width/2,pos.y-cc.winSize.height/2,0);
        this.hero.setPosition(pos.x - view.getVisibleSize().width / 2, pos.y - view.getVisibleSize().height / 2, 0);
        // console.log(this.hero.position.x);

    }

    update(deltaTime: number) {
        // [4]
        if (this.heroMove) {

            this.hero.setPosition(this.hero.position.x + this.moveNum[0] * 2, this.hero.position.y + this.moveNum[1] * 2, 0);
            this.node.setPosition(this.node.position.x + this.moveNum[0] * -2, this.node.position.y + this.moveNum[1] * -2, 0);

        }
    }
    onDestroy() {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
